package com.example.chenhuamou.freenews.view.mine;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chenhuamou.freenews.R;
import com.example.chenhuamou.freenews.view.common.Constants;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.share.WbShareCallback;
import com.sina.weibo.sdk.share.WbShareHandler;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenhuamou on 2017/9/14.
 */

public class MineFragment extends android.support.v4.app.Fragment implements View.OnClickListener, WbShareCallback {

    Dialog mDialog;
    TextView tv_wx;
    TextView tv_qq;

    // 新浪微博
    private WbShareHandler shareHandler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.bind(this, view);
        initWBSDK();
        return view;
    }

    private void initWBSDK() {
        WbSdk.install(getActivity(), new AuthInfo(getActivity(), Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE));
        shareHandler = new WbShareHandler(getActivity());
        shareHandler.registerApp();

    }

    @OnClick({R.id.share})
    public void OnClick(View view) {
        mDialog = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);
        View shareView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_share, null);
        tv_wx = shareView.findViewById(R.id.share_wx);
        tv_wx.setOnClickListener(this);
        mDialog.setContentView(shareView);
        // 获取窗口
        Window window = mDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.y = 40;
        window.setAttributes(layoutParams);
        mDialog.show();
    }

    @Override
    public void onClick(View view) {
        mDialog.dismiss();
        WeiboMultiMessage weiboMultiMessage = new WeiboMultiMessage();
        TextObject textObject = new TextObject();
        textObject.text = "666，这就是我的freestyle！";
        weiboMultiMessage.textObject = textObject;
        shareHandler.shareMessage(weiboMultiMessage, false);

    }

    // 微博分享回调
    @Override
    public void onWbShareSuccess() {
        Toast.makeText(getActivity(), "分享成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onWbShareCancel() {
        Toast.makeText(getActivity(), "取消分享", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onWbShareFail() {
        Toast.makeText(getActivity(), "分享失败", Toast.LENGTH_SHORT).show();
    }
}
